// src/api/SysUserAPI.js
import http from '@/utils/http'

/**
 * 添加系统用户
 * @param {Object} params - 用户信息对象
 */
export const addSysUserAPI = (params) => {
  return http.post('/sys-client/cms/sysUser/add', params)
}

/**
 * 修改系统用户资料
 * @param {Object} params - 包含 id 和用户信息的对象
 */
export const updateSysUserAPI = (params) => {
  return http.post(`/sys-client/cms/sysUser/update?id=${params.id}`, params.saveBO)
}

/**
 * 切换用户状态（0->1, 1->0）
 * @param {number} id - 用户ID
 */
export const toggleUserStatusAPI = (id) => {
  return http.post(`/sys-client/cms/sysUser/toggleStatus?id=${id}`)
}

/**
 * 删除单个系统用户
 * @param {number} id - 用户ID
 */
export const deleteSysUserAPI = (id) => {
  return http.post(`/sys-client/cms/sysUser/delete?id=${id}`)
}

/**
 * 批量删除系统用户
 * @param {string} ids - 以逗号分隔的ID字符串
 */
export const deleteBatchSysUserAPI = (ids) => {
  return http.post(`/sys-client/cms/sysUser/deleteBatch?ids=${ids}`)
}

/**
 * 根据ID获取用户详情
 * @param {number} id - 用户ID
 */
export const getSysUserByIdAPI = (id) => {
  return http.post(`/sys-client/cms/sysUser/get?id=${id}`)
}

/**
 * 获取所有系统用户列表（带角色信息）
 */
export const listAllSysUsersAPI = (params) => {
  console.log('🚀 发送请求:', {
    url: '/sys-client/cms/sysUser/listAll',
    method: 'post',
    data: params
  })
  return http.post('/sys-client/cms/sysUser/listAll', params).then(res => {
    console.log('✅ 接收到响应:', res)
    return res
  }).catch(err => {
    console.error('❌ 请求失败:', err)
    throw err
  })
}

/**
 * 分页获取普通员工列表
 * @param {number} pageNo - 页码
 * @param {number} pageSize - 每页大小
 */
export const getUsersWithUserRoleByPageAPI = (pageNo, pageSize) => {
  return http.post('/sys-client/cms/sysUser/getUsersWithUserRoleByPage', {}, {
    params: { 
      pageNo: pageNo || 1, 
      pageSize: pageSize || 10 
    }
  })
}

/**
 * 获取所有状态启用的角色列表（不分页）
 * 不传参
 */
export const listAllNoPageAPI = () => {
  return http.post('/sys-client/cms/sysRole/listAllNoPage')
}

/**
 * 为系统用户分配角色（一个用户绑定一个角色，一个角色可以被分配给多个用户）
 *
 */
export const assignRoleAPI = (userId, roleId) => {
  return http.post('/sys-client/cms/sysUser/assign/role', {}, {
    params: {
      userId: userId,
      roleId: roleId
    }
  })
}

/**
 * 解除用户和角色的绑定
 * @param {number} userId - 用户ID
 * @param {number} roleId - 角色ID
 */
export const unassignRoleAPI = (userId, roleId) => {
  return http.post('/sys-client/cms/sysUser/assign/unassignRole', {}, {
    params: {
      userId: userId,
      roleId: roleId
    }
  })
}

/**
 * 根据用户名模糊查询用户列表
 * @param {Object} params - 查询参数对象
 */
export const searchByUserNameAPI = (params) => {
  return http.post('/sys-client/cms/sysUser/searchByUserName', params)
}